package com.taobao.ic.ticket.dao.ibatis;

import java.sql.SQLException;

import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import com.taobao.ic.ticket.dao.Param;
import com.taobao.ic.ticket.dao.UserDao;
import com.taobao.ic.ticket.domain.UserDO;
import com.taobao.ic.ticket.exception.DaoException;

public class IbatisUserDao extends SqlMapClientDaoSupport implements UserDao {

	@Override
	public UserDO getById(long userId) throws DaoException {
		Param param = Param.create().add("user_id", userId);
		try {
			return (UserDO) getSqlMapClient().queryForObject("IbatisUserDao.getById", param);
		} catch (SQLException e) {
			throw new DaoException("query user by id failed!",e);
		}
	}

	@Override
	public UserDO getByUsername(String username) throws DaoException {
		Param param = Param.create().add("username", username);
		try {
			return (UserDO) getSqlMapClient().queryForObject("IbatisUserDao.getByUsername", param);
		} catch (SQLException e) {
			throw new DaoException("query user by username failed!",e);
		}
	}

	@Override
	public UserDO register(UserDO userDo) throws DaoException {
		
		try {
			return (UserDO) getSqlMapClient()
					.insert("IbatisUserDao.register", userDo);
		} catch (SQLException e) {
			throw new DaoException("create User failed!", e);
		}
		
	}

	@Override
	public long generateUserId() throws DaoException {
		
			try {
				return (Long) getSqlMapClient().queryForObject(
						"IbatisUserDao.generateUserId");
			} catch (SQLException e) {
				throw new DaoException("generate ticketId failed!", e);
			}
		
	}

}
